package p2pserver
{
	import flash.events.NetStatusEvent;
	import flash.net.NetConnection;	

	
	public class P2pNetConnection extends NetConnection
	{
		private var developKey:String = "93757daf7a1b31f37867aa1e-c714c797ce7e";
		private var _p2pUrl:String = null;
		
		private var netConnectionEventProcess:IP2pNetConnectionEventProcess;
		private var connectSuccess:Function = null;
		private var connectFailure:Function = null;
		private var streamConnSuccess:Function = null;
		private var groupConnSuccess:Function = null;
		
		public function P2pNetConnection(netConnectionEventProcess:IP2pNetConnectionEventProcess)
		{
			super();
			
			this.netConnectionEventProcess = netConnectionEventProcess;
			this.streamConnSuccess = streamConnSuccess;
			this.groupConnSuccess = groupConnSuccess;
		}
		
		public function get P2pUrl():String
		{
			return _p2pUrl;
		}

		public function Connect(p2pUrl:String):void{

			_p2pUrl = p2pUrl;
			
			this.addEventListener(NetStatusEvent.NET_STATUS, NetStatus);
			//this.connect(_p2pUrl+developKey);
			this.connect(_p2pUrl);
		}
		
		protected function ConnectSuccess():void{
			
			netConnectionEventProcess.onNetConnectSuccess();
		}
		
		protected function ConnectFailure():void{
		
			netConnectionEventProcess.onNetConnectFailure();
		}
		
		protected function ConnectClosed():void{
			netConnectionEventProcess.onNetConnectClosed();
		}
		
		protected function StreamConnectSuccess():void{

			netConnectionEventProcess.onNetStreamConnectSuccess();
		}
		
		protected function GroupConnectSuccess():void{
			
			netConnectionEventProcess.onGroupConnectSuccess();
		}
		
		private function NetStatus(event:NetStatusEvent):void{
			trace(event.info.code);
			
			switch(event.info.code){
				case "NetConnection.Connect.Success":
					ConnectSuccess();
					break;
				case "NetConnection.Connect.Failed":
					ConnectFailure();
					break;
				case "NetConnection.Connect.Closed":
					ConnectClosed();
					break;
				case "NetStream.Connect.Success":
					StreamConnectSuccess();
					break;
				case "NetGroup.Connect.Success":
					GroupConnectSuccess();
					break;
			}
		}
			
	}
}